//
//  MLAPropertySubletApplyVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/20.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAPropertySubletApplyVC.h"
#import "MLABaseFieldModel.h"
#import "MLABaseFieldView.h"
#import "MLAPropertySubletModel.h"

@interface MLAPropertySubletApplyVC ()

@end

@implementation MLAPropertySubletApplyVC {
    NSArray<MLABaseFieldModel *> *modelsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configTitle:@"请您填写申请信息"];
    
    [self addSubviewsInView];
}

#pragma mark - LoadData
- (void)loadDataWithParams:(NSDictionary *)params {
    
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kSubletApply params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        if (self.applySuccessBlock) {
            self.applySuccessBlock();
        }
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^{
        
    }];
}

#pragma mark - Actions
- (void)commitBtnAction {
    CX_Log(@"发起转租");
    
    NSString *money = [self getContentWithTag:12000];
    NSString *name = [self getContentWithTag:12001];
    NSString *phone = [self getContentWithTag:12002];
    NSString *remark = [self getContentWithTag:12003];
    
    if (name.length == 0) {
        [MLAHudManage showMessage:@"请输入申请人"];
        return;
    }
    
    if (phone.length != 11) {
        [MLAHudManage showMessage:@"请输入手机号"];
        return;
    }
    
    if (remark.length == 0) {
        [MLAHudManage showMessage:@"请输入备注"];
        return;
    }
    
    if (!money || money.length == 0) {
        money = @"";
    } else {
        money = [NSString stringWithFormat:@"%ld",money.integerValue*100];
    }
    
    NSDictionary *params = @{@"month_rent":money,
                             @"shop_id":_model.modelId,
                             @"apply_name":name,
                             @"apply_mobile":phone,
                             @"content":remark};
    [self loadDataWithParams:params];

}

- (NSString *)getContentWithTag:(NSInteger)tag {
    MLABaseFieldView *fieldView = (MLABaseFieldView *)[self.tableView.tableHeaderView viewWithTag:tag];
    return fieldView.inputText;
}

#pragma mark - Subviews
- (void)addSubviewsInView {
    
    modelsArr = [self getModelsArr];
    
    CGFloat height = 430;
    
    UIView *tableHeaderView = [[UIView alloc] init];
    tableHeaderView.backgroundColor = CX_GetColorHex(0xFCFCFC, 1);
    tableHeaderView.frame = CGRectMake(0, 0, CX_GetScreenWidth(), height);
    
    [modelsArr enumerateObjectsUsingBlock:^(MLABaseFieldModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        MLABaseFieldView *fieldView = [[MLABaseFieldView alloc] init];
        fieldView.tag = 12000+idx;
        fieldView.model = obj;
        [tableHeaderView addSubview:fieldView];
        
        if (idx == 0) {
            [fieldView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(20);
                make.left.right.equalTo(tableHeaderView);
                make.height.mas_equalTo(85);
            }];
        } else {
            [fieldView mas_makeConstraints:^(MASConstraintMaker *make) {
                UIView *view = [tableHeaderView viewWithTag:12000+idx-1];
                make.left.right.height.equalTo(view);
                make.top.equalTo(view.mas_bottom);
            }];
        }
    }];
    
    UIButton *commitBtn = [self getCommitButtonWithTitle:@"发起"];
    [commitBtn addTarget:self action:@selector(commitBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [tableHeaderView addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        UIView *view = [tableHeaderView viewWithTag:12000+self->modelsArr.count-1];
        make.top.equalTo(view.mas_bottom).offset(10);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(40);
    }];
    
    self.tableView.tableHeaderView = tableHeaderView;
    [tableHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.width.mas_equalTo(CX_GetScreenWidth());
        make.height.mas_equalTo(height);
    }];
    
}

- (NSArray<MLABaseFieldModel *> *)getModelsArr {
    
    NSArray *infosArr = @[CX_GetFieldInfoDic(@"月租金：", @"请输入租金金额", @"30", @"0", @"1", @"0", @""),
                          CX_GetFieldInfoDic(@"申请人：", @"输入申请人", @"30", @"0", @"1", @"0", @""),
                          CX_GetFieldInfoDic(@"电话：", @"输入手机号", @"11", @"0", @"1", @"0", @""),
                          CX_GetFieldInfoDic(@"备注：", @"输入备注信息", @"30", @"0", @"1", @"0", @"")];
    return [[MLABaseFieldModel mj_objectArrayWithKeyValuesArray:infosArr] copy];
}





@end
